<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物清单比较工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7fa;
        }

        h1,
        h2,
        h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-top: 20px;
        }

        .card {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }

        .full-width {
            grid-column: 1 / -1;
        }

        .list-container {
            margin-top: 15px;
        }

        .list-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
        }

        .list-item:last-child {
            border-bottom: none;
        }

        .list-item button {
            background: #e74c3c;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 2px 8px;
            cursor: pointer;
        }

        textarea {
            width: 100%;
            height: 120px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 10px;
            resize: vertical;
        }

        button {
            background: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 8px 15px;
            cursor: pointer;
            transition: background 0.3s;
        }

        button:hover {
            background: #2980b9;
        }

        .result-item {
            background-color: #f8f9fa;
            padding: 10px;
            margin: 5px 0;
            border-radius: 4px;
            border-left: 4px solid #3498db;
        }

        .empty-message {
            color: #7f8c8d;
            font-style: italic;
            text-align: center;
            padding: 20px;
        }

        .instructions {
            background-color: #eaf7ff;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }

        .example-btn {
            background: #2ecc71;
            margin-right: 10px;
        }

        .clear-btn {
            background: #e67e22;
        }

        .action-buttons {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="card">
        <h1>购物清单比较工具</h1>
        <div class="instructions">
            <p>这个工具可以帮助您比较两个购物清单，找出第一个清单中独有的商品。</p>
            <p>使用场景：比较不同电商平台的购物车、对比不同时期的采购清单、查找缺失商品等。</p>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <h2>清单 A（主清单）</h2>
            <textarea id="list-a" placeholder="输入商品，每行一个商品名称"></textarea>
            <div class="action-buttons">
                <button class="example-btn" onclick="loadExample(1)">加载示例</button>
                <button class="clear-btn" onclick="clearList('list-a')">清空</button>
            </div>
        </div>

        <div class="card">
            <h2>清单 B（对比清单）</h2>
            <textarea id="list-b" placeholder="输入商品，每行一个商品名称"></textarea>
            <div class="action-buttons">
                <button class="example-btn" onclick="loadExample(2)">加载示例</button>
                <button class="clear-btn" onclick="clearList('list-b')">清空</button>
            </div>
        </div>

        <div class="card full-width">
            <h2>比较结果</h2>
            <p>以下是仅在清单A中存在的商品：</p>
            <div id="result-container" class="list-container">
                <div class="empty-message">请先输入并比较两个清单</div>
            </div>
            <button onclick="compareShoppingLists()">比较清单</button>
        </div>
    </div>

    <script>
        // 差集函数 - 返回存在于数组a但不存在于数组b的元素
        const difference = (a, b) => {
            const s = new Set(b);
            return a.filter(x => !s.has(x));
        };

        // 比较购物清单
        function compareShoppingLists() {
            // 获取两个清单的内容
            const listA = document.getElementById('list-a').value
                .split('\n')
                .map(item => item.trim())
                .filter(item => item !== '');

            const listB = document.getElementById('list-b').value
                .split('\n')
                .map(item => item.trim())
                .filter(item => item !== '');

            // 使用difference函数找出仅在清单A中存在的商品
            const uniqueItems = difference(listA, listB);

            // 显示结果
            const resultContainer = document.getElementById('result-container');

            if (uniqueItems.length === 0) {
                resultContainer.innerHTML = '<div class="empty-message">没有找到仅在清单A中存在的商品</div>';
                return;
            }

            let resultHTML = '';
            uniqueItems.forEach(item => {
                resultHTML += `<div class="result-item">${item}</div>`;
            });

            resultContainer.innerHTML = resultHTML;
        }

        // 加载示例数据
        function loadExample(listNumber) {
            const exampleListA = [
                "苹果手机",
                "华为平板",
                "小米耳机",
                "戴尔笔记本",
                "索尼相机",
                "三星电视",
                "罗技鼠标",
                "飞利浦电动牙刷"
            ].join('\n');

            const exampleListB = [
                "苹果手机",
                "华为平板",
                "联想笔记本",
                "佳能相机",
                "三星电视",
                "雷蛇鼠标"
            ].join('\n');

            if (listNumber === 1) {
                document.getElementById('list-a').value = exampleListA;
            } else {
                document.getElementById('list-b').value = exampleListB;
            }
        }

        // 清空列表
        function clearList(listId) {
            document.getElementById(listId).value = '';
        }
    </script>
</body>

</html>